#include "StdAfx.h"
#include "Melee.h"
#include "StateFactory.h"
#include "../kernel.h"

extern Kernel* g_kernel;

#define TIME g_kernel->GetTime()

Melee::Melee(void) : IWeapon()
{
    m_attributes.Put( "weapontime", 400.0f );
    m_attributes.Put( "damage", 30 );
    m_attributes.Put( "damage2", 50 );
    m_attributes.Put( "damage3", 60 );
}

Melee::~Melee(void)
{
}

/** Fire this weapon */
void Melee::Fire()
{
    if ( IsReady() )
    {
        SetState( "WP_FIRING" );
        std::string currentState =  m_owner->GetState();

        int damage = 0;
        if( currentState == ENT_STATES[ ATTACKING1] )
        {
            m_owner->SetState( "ATTACKING2" );
            damage = m_attributes.GetInt( "damage2" );
        }
        else if ( currentState == ENT_STATES[ ATTACKING2 ] )
        {
            m_owner->SetState( "ATTACKING3" );
            damage = m_attributes.GetInt( "damage3" );
        } 
        else if ( currentState == ENT_STATES[ ATTACKING3 ] )
        {
            m_owner->SetState( "ATTACKING1" );
            damage = m_attributes.GetInt( "damage" );            
        }
        else
        {
            m_owner->SetState( "ATTACKING1" );
            damage = m_attributes.GetInt( "damage" );
        }
 
        IEntity* ent = EntityManager::GetInstance()->FindInRegion( m_owner->GetReachRectangle(), m_owner );
        if ( ent )  ent->Damage( -damage );

        m_weaponTime = TIME + m_attributes.GetFloat("weapontime");
    }
    else
    {
        SetState( "WP_RELOADING" );
    }
}